/********************************************************************************
 * Project Name		[JavaEE_Web]
 * File Name     	[DownloadFileServlet.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package servlet.cases;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 下载文件
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
@WebServlet(name = "DownloadFileServlet", urlPatterns = "/download")
public class DownloadFileServlet extends HttpServlet {

	private static final long serialVersionUID = -816858255510998793L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// this.downloadBinaryFile(response);
		this.downloadTextFile(response);
	}

	/**
	 * 下载二进制文件, 如图片<br/>
	 * 使用OutputStream<br/>
	 * 
	 * @param response
	 * @throws IOException
	 */
	protected void downloadBinaryFile(HttpServletResponse response) throws IOException {
		// 获取下载的文件信息
		String filePath = this.getServletContext().getRealPath("/images/美国队长.jpg");
		String fileName = filePath.substring(filePath.lastIndexOf("\\") + 1);
		// 中文文件名要编码
		response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));

		// 读取文件并向页面发送
		OutputStream out = response.getOutputStream();
		InputStream in = new FileInputStream(filePath);
		byte[] buffer = new byte[1024];
		int length = 0;
		while ((length = in.read(buffer)) != -1) {
			out.write(buffer, 0, length);
		}
		in.close();
	}

	/**
	 * 下载文本文件<br/>
	 * 使用PrintWriter<br/>
	 * 
	 * @param response
	 * @throws IOException
	 */
	protected void downloadTextFile(HttpServletResponse response) throws IOException {
		// 获取下载的文件信息
		String filePath = this.getServletContext().getRealPath("/WEB-INF/web.xml");
		String fileName = filePath.substring(filePath.lastIndexOf("\\") + 1);
		response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));

		// 读取文件并向页面发送
		PrintWriter writer = response.getWriter();
		BufferedReader reader = new BufferedReader(new FileReader(filePath));
		String line = "";
		while ((line = reader.readLine()) != null) {
			writer.println(line);
		}
		reader.close();
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}
}
